package com.kingbook.services;

import com.google.common.collect.Maps;
import com.kingbook.core.base.SupportService;
import com.kingbook.core.result.JSONResult;
import com.kingbook.entity.Vehicle;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author Bucky
 * @Date 2019-12-18 21:37:35
 * @Version 1.0
 */
@Slf4j
@Service
public class VehicleService extends SupportService {

    @Autowired
    DriverService driverService;
    @Autowired
    TonsService tonsService;
    @Autowired
    CategoryService categoryService;

    @PostConstruct
    private void init() {
        setNamespace("Vehicle");
    }

    public JSONResult updateVehicle(Map<String, Object> param) {
        int mid = MapUtils.getIntValue(param, "mid");
        int id = MapUtils.getIntValue(param, "id");
        int tons = MapUtils.getIntValue(param, "tons");
        int jib = MapUtils.getIntValue(param, "jib");
        int amount = MapUtils.getIntValue(param, "amount");
        String plateNo = MapUtils.getString(param, "plateNo");
        String driverName = MapUtils.getString(param, "driver");
        param.put("did", driverService.getDriverId(mid, driverName, true));
        tonsService.updateTonsName(mid, tons, jib, amount);
//        categoryService.updateCategoryName(mid, BRAND, MapUtils.getString(param, "brand"));
//        categoryService.updateCategoryName(mid, MODEL, MapUtils.getString(param, "model"));
//        categoryService.updateCategoryName(mid, insurance_company, MapUtils.getString(param, "insuranceCompany"));
        param.put("mid", mid);
        param.put("pid", 0);
        param.put("plateNo", plateNo);
        Vehicle vehicle = findObject("queryVehicleByPlateNo", param, Vehicle.class);
        if (vehicle != null && vehicle.getId() !=id && plateNo.equals(vehicle.getPlateNo()))
            return JSONResult.failed(plateNo + " 已注册");
        return id > 0 ? modify(param) : save(param);
    }

    public int getVehicleId(int mid, int pid, String plateNo) {
        Map<String, Object> param = new HashMap<>(2);
        param.put("mid", mid);
        param.put("pid", pid);
        param.put("plateNo", plateNo);
        Vehicle vehicle = findObject("queryVehicleByPlateNo", param, Vehicle.class);
        if (vehicle != null) {
            return vehicle.getId();
        } else {
            // 为合作伙伴添加车辆到车辆表
            JSONResult result = insertReturnKey("insert", param);
            return Integer.parseInt(result.getMsg());
        }
    }

    public int getRows(int id, int mid) {
        Map<String, Object> param = Maps.newHashMap();
        param.put("id", id);
        param.put("mid", mid);
        return getRows("getRows", param);
    }

    public JSONResult queryVehicleInfo(Map<String, Object> param) {
        return query("queryVehicleInfo", param);
    }

    public Map<String, Object>[] getDistinctBrands(Map<String, Object> param) {
        return queryMap("getDistinctBrands", param);
    }

    public Map<String, Object>[] getDistinctModels(Map<String, Object> param) {
        return queryMap("getDistinctModels", param);
    }

    public Map<String, Object>[] getDistinctInsuranceCompany(Map<String, Object> param) {
        return queryMap("getDistinctInsuranceCompany", param);
    }
}
